﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;

public partial class ucControl_ucXecu : System.Web.UI.UserControl
{
    TblMenuChildDAO menuChildDAO = new TblMenuChildDAO(Global.CONNECTION_STRING);
    TblMenuParrentDAO menuPaDAO = new TblMenuParrentDAO(Global.CONNECTION_STRING);
    TblItemsDAO itemDAO = new TblItemsDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadMenuPa();
            LoadItem();
            LoadItemLeft();
            LoadDongXe();
            string idP = Request.QueryString["idP"];
            if (idP != null || idP != "")
            {
                LoadItem_ByIDParrent();
            }
        }
    }

    private void LoadDongXe()
    {
        rpDongxe.DataSource = menuPaDAO.GetAll();
        rpDongxe.DataBind();
    }

    private void LoadMenuPa()
    {
        rpMenuPa.DataSource = menuPaDAO.GetAll();
        rpMenuPa.DataBind();
    }

    private void LoadItemLeft()
    {
        rpItemLEft.DataSource = itemDAO.GetItem_New();
        rpItemLEft.DataBind();
    }

    protected void rpMenuPa_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater reChildMenuLeft = (Repeater)e.Item.FindControl("rpmenuChild");
            HiddenField hidIDMenuPLeft = (HiddenField)e.Item.FindControl("hid");
            hidIDMenuPLeft.Value = DataBinder.Eval(e.Item.DataItem, "IDMenuParent").ToString();

            string id = hidIDMenuPLeft.Value;
            if (reChildMenuLeft != null)
            {
                reChildMenuLeft.DataSource = menuChildDAO.GetCateChild_ByIDParrent(Global.ConvertToInt(id));
                reChildMenuLeft.DataBind();
            }
        }
    }

    private void LoadItem()
    {

        lvItems.DataSource = itemDAO.GetAllProduct_New(2, 1, 1);
            lvItems.DataBind();
        
    }

    private void LoadItem_ByIDParrent()
    {
        string id = Request.QueryString["idP"];
        if (Global.ConvertToInt(id) != 0)
        {
            lvItems.DataSource = itemDAO.GetAllProduct_ByMenuParrent(Global.ConvertToInt(id));
            lvItems.DataBind();
        }
    }

    protected void lvItems_ItemDataBound(object sender, ListViewItemEventArgs e)
    {

    }
    protected void lvItems_PagePropertiesChanged(object sender, EventArgs e)
    {

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        lvItems.DataSource = itemDAO.SapXepSP(Global.ConvertToInt(drTangGiam.SelectedValue), Global.ConvertToInt(drSelect.SelectedValue));
        lvItems.DataBind();
    }
}
